package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.ExportProgress;
import com.ruoyi.system.domain.TEquipment;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author : Xuzhaobo
 * @date : 2025-02-13 20:57
 * @description :
 **/
@Service
public class ExportProgressService {
    private final Map<String, ExportProgress> progressMap = new HashMap<>();

    public String startExport(TEquipment tEquipment) {
        String progressId = generateProgressId();
        progressMap.put(progressId, new ExportProgress());
        // 启动异步导出任务
        new Thread(() -> {
            try {
                // 模拟导出过程
                Thread.sleep(5000); // 模拟5秒导出时间
                completeExport(progressId);
            } catch (InterruptedException e) {
                failExport(progressId, e.getMessage());
            }
        }).start();
        return progressId;
    }

    public void completeExport(String progressId) {
        ExportProgress progress = progressMap.get(progressId);
        if (progress != null) {
            progress.setStatus("COMPLETED");
        }
    }

    public void failExport(String progressId, String errorMessage) {
        ExportProgress progress = progressMap.get(progressId);
        if (progress != null) {
            progress.setStatus("FAILED");
            progress.setErrorMessage(errorMessage);
        }
    }

    public ExportProgress getExportProgress(String progressId) {
        return progressMap.get(progressId);
    }

    private String generateProgressId() {
        return "progress-" + System.currentTimeMillis();
    }
}
